package com.devbobcorn.nekorization.client.event;

import com.devbobcorn.nekorization.Nekorization;
import com.devbobcorn.nekorization.client.model.NekoModel;
import com.devbobcorn.nekorization.client.rendering.SpriteOnShoulderRenderer;
import com.devbobcorn.nekorization.entities.NekoEntity;
import com.mojang.blaze3d.vertex.PoseStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;

// Client-Side Only Things...
@EventBusSubscriber(modid = Nekorization.MODID, bus = EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ClientForgeEventSubscriber {
	@SubscribeEvent
	public static void onPreLivingRenderEvent(RenderLivingEvent.Pre<NekoEntity, NekoModel<NekoEntity>> event) {
		LivingEntity entity = event.getEntity();
		if (!(entity instanceof NekoEntity))
			return;
		float scale = 0.7F;
		PoseStack stack = event.getMatrixStack();
		stack.scale(scale, scale, scale);
		if (((NekoEntity) entity).isInSittingPose())
			stack.translate(0.0, -0.45, 0.0);
	}

	private static SpriteOnShoulderRenderer spriteOnShoulderRenderer = null;

	@SubscribeEvent
    public static void onRenderPlayerPost(RenderPlayerEvent.Post event) {
		if (spriteOnShoulderRenderer == null) {
			spriteOnShoulderRenderer = new SpriteOnShoulderRenderer(Minecraft.getInstance().getEntityModels().bakeLayer(ClientModEventSubscriber.SPRITE));
		} else {
			Player player = event.getPlayer();
			PoseStack stack = event.getMatrixStack();
			MultiBufferSource buffers = event.getBuffers();
			spriteOnShoulderRenderer.render(stack, buffers, event.getLight(), player, 1.0F, 1.0F, 1.0F, 1.0F);
		}
    }
}
